package com.link.base.base.saleactivity.controller;
/*
 * createdby: wangdawei
 * created: 2018-11-20
 * comments: 营销活动-活动监控
 */

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.link.base.user.model.CoreUser;
import com.link.base.base.saleactivity.model.AccntCampMonitor;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.ServiceException;
import com.link.core.modules.export.ExportFactory;
import com.link.core.modules.export.ExportWriter;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.link.base.base.saleactivity.service.AccntCampMonitorService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/link/accntCampMonitor")
public class AccntCampMonitorController extends BasicController<AccntCampMonitor> {
 
    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private AccntCampMonitorService accntCampMonitorService;

    @Override
    public BasicService<AccntCampMonitor> getBasicService() throws Exception {
        return accntCampMonitorService;
    }

    /**
     * 查询客户参与活动列表
     * @author 王艳
     * @date 2018年12月19日
     */
    @RequestMapping({"/queryListPage"})
    @ResponseBody
    public Map<String, Object> queryListPage(@JsonParam QueryParams qps, @JsonParam AccntCampMonitor entity,
                                             HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        HashMap result = new HashMap();

        try {
            qps.preDealSecurity(request);
            AccntCampMonitor temp = (AccntCampMonitor) BasicModel.transformClass(entity, qps);
            this.beforQueryAllOrExample(temp, request);
            this.beforQueryExample(temp, request);
            List<AccntCampMonitor> list = accntCampMonitorService.queryListPage(temp);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount(temp, list.size()));
        } catch (ServiceException var9) {
            result.put("success", false);
            result.put("rows", new ArrayList());
            result.put("result", var9.getMessage());
            result.put("detailMessage", var9.getMessage());
        } catch (Exception var10) {
            result.put("success", false);
            result.put("rows", new ArrayList());
            result.put("result", var10.getMessage());
        }

        return result;
    }

    /**
     * 客户参与活动列表导出
     * @author 王艳
     * @date 2018年12月19日
     */
    @RequestMapping({"/queryListPage/exportData"})
    @ResponseBody
    public Map<String, Object> exportListData(@JsonParam AccntCampMonitor entity, @JsonParam QueryParams qps,
                                              HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        HashMap result = new HashMap();

        try {
            qps.preDealSecurity(request);
            CoreUser user = UserUtil.getUser();
            if (null != user) {
                user.setExportType(qps.getExportFileType());
            }

            ExportWriter exportWriter;
            if ("xlsx".equals(user.getExportType())) {
                exportWriter = ExportFactory.newExcelInstance(qps);
            } else {
                exportWriter = ExportFactory.newInstance(qps);
            }

            AccntCampMonitor temp = (AccntCampMonitor) BasicModel.transformClass(entity, qps);

            while (!qps.getStopExport()) {
                this.beforQueryAllOrExample(temp, request);
                this.beforQueryExample(temp, request);
                List<AccntCampMonitor> list = accntCampMonitorService.queryListPage(temp);
                if (qps.getPage() == 1) {
                    qps.setTotal(temp.getTotal());
                }

                exportWriter.wirteData(list);
                if ("Y".equals(qps.getExportCurrentPage())) {
                    break;
                }

                temp.setPage(qps.getPage());
                temp.setTotalFlag(qps.getTotalFlag());
            }

            result.put("success", true);
            result.put("url", exportWriter.getUrlPath());
        } catch (ServiceException var11) {
            result.put("success", false);
            result.put("result", var11.getMessage());
            result.put("detailMessage", var11.getMessage());
        } catch (Exception var12) {
            result.put("success", false);
            result.put("result", var12.getMessage());
        }

        return result;
    }

    /**
     * 查询客户参与活动详情
     * @author 王艳
     * @date 2018年12月19日
     */
    @RequestMapping({"/queryDetailById"})
    @ResponseBody
    public Map<String, Object> queryDetailById(@JsonParam AccntCampMonitor entity,
                                               HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        HashMap result = new HashMap();

        try {
            AccntCampMonitor record = accntCampMonitorService.queryDetailById(entity);
            List<AccntCampMonitor> list = new ArrayList();
            list.add(record);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("result", record);
            result.put("success", true);
        } catch (ServiceException var8) {
            result.put("success", false);
            result.put("result", var8.getMessage());
            result.put("detailMessage", var8.getMessage());
        } catch (Exception var9) {
            result.put("success", false);
            result.put("result", var9.getMessage());
        }

        return result;
    }
}
